import { defineConfig, loadEnv } from 'vite'
import path from 'path'
import { createVitePlugins } from './build/plugin'
import { createProxy } from './build/utils'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const isBuild = command === 'build'
  // console.log(isBuild)

  const env = loadEnv(mode, process.cwd())
  const viteEnv = loadEnv(mode, process.cwd())
  const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = viteEnv
  return {
    base: VITE_PUBLIC_PATH || '/',
    resolve: {
      alias: {
        vue: 'vue/dist/vue.esm-bundler.js', // vue内部需要定义组件（menu中）
        '@': path.resolve(__dirname, 'src'),
      },
    },
    plugins: createVitePlugins(viteEnv, isBuild),
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "./src/styles/design/constant.scss" as *;`,
        },
      },
    },
    server: {
      host: '0.0.0.0',
      open: true,
      port: VITE_PORT,
      proxy: createProxy(VITE_PROXY),
    },
    clearScreen: false, // vite清屏不清除控制台打印的信息
    // logLevel:'error',
    build: {
      target: 'es2015',
      sourcemap: false,
      reportCompressedSize: false, // 启用/禁用 gzip 压缩大小报告
      chunkSizeWarningLimit: 1024, // chunk 大小警告的限制（单位kb）
      commonjsOptions: {
        ignoreTryCatch: false,
      },
    },
  }
})
